
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_common/flutter_common.dart';
import 'package:flutter_common/util/context_extension.dart';
import 'package:flutter_common/util/string_utils.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

///[controller] 用于修改[content]文案的调整
Future showLoadingDialog<T>(BuildContext context,
    {String? content, bool outsideCancel = true, Future<T>? future, ValueNotifier<String>? controller}) {
  return showDialog(
      context: context,
      barrierColor: Colors.black12,
      barrierDismissible: outsideCancel,
      routeSettings: const RouteSettings(name: "loading"),
      builder: (context) {
        Widget w = LoadingDialog(future:future, content:content,controller: controller);
        if (!outsideCancel) {
          w = WillPopScope(child: w, onWillPop: () => Future.value(false));
        }
        return w;
      });
}

void dismissLoadingDialog(BuildContext? context) {
  if(context == null) return;
  if(FxTheme().isDialogShow && context.mounted) {
    Navigator.of(context).pop();
  }
}

class LoadingDialog<T> extends StatefulWidget {

  final Future<T>? future;
  final String? content;
  final ValueNotifier<String>? controller;

  const LoadingDialog({super.key, this.future, this.content, this.controller});

  @override
  State<StatefulWidget> createState() => _State();

}

class _State extends State<LoadingDialog> {

  bool get _isLoading => "loading".equals(FxTheme().dialogType);

  @override
  Widget build(BuildContext context) {
    if(widget.future == null) {
      return _mask();
    } else {
      return FutureBuilder(builder: (context, snapshot) {
        // logger.error(FxTheme().isDialogShow);
        if(_isLoading && snapshot.connectionState == ConnectionState.done) {
          Future.microtask(() => Navigator.pop(context, snapshot.data));
        }
        return _mask();
      }, future: widget.future);
    }
  }

  _mask() {
    return Center(
        child: Container(
          constraints: BoxConstraints(minWidth: 240.w, minHeight: 240.w, maxWidth: 480.w),
          decoration: BoxDecoration(
              color: const Color(0xAA000000),
              borderRadius: BorderRadius.circular(8)),
          padding: const EdgeInsets.all(10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              SizedBox(height: 60.w),
              CupertinoActivityIndicator(
                radius: 30.w,
                animating: true,
                color: Colors.white,
              ),
              SizedBox(height: 30.w),
              _text()
            ],
          ),
        ));
  }

  _text() {
    if (widget.controller == null) {
      return Text(widget.content??'请稍候',
          style: TextStyle(
              color: Colors.white,
              fontSize: fontSize32,
              decoration: TextDecoration.none));
    } else {
      return ValueListenableBuilder<String>(
          valueListenable: widget.controller!,
          builder: (context, value, widget) {
            return Text(value,
                style: TextStyle(
                    color: Colors.white,
                    fontSize: fontSize32,
                    decoration: TextDecoration.none));
          });
    }
  }
}